package com.controller.wx;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.*;

import javax.servlet.http.HttpServletRequest;

import com.alibaba.fastjson.JSONArray;
import com.common.http.HttpResult;
import com.common.model.Global;
import com.common.model.UserTypeEnum;
import com.common.spring.SpringMvcUtils;
import com.dao.qm.PartsDao;
import com.dao.qm.PartsDetailDao;
import com.dao.qm.ProcessDao;
import com.model.qm.PartsDetailModel;
import com.model.qm.PartsModel;
import com.model.qm.ProcessModel;
import com.service.qm.PartsDetailService;
import com.service.qm.PartsService;
import com.service.sys.WXService;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.common.DateUtils;
import com.common.DesUtils;
import com.common.StringUtils;
import com.common.db.RedisUtils;
import com.common.wx.WechatUtil;
import com.dao.sys.SysUserMapper;
import com.dao.task.TaskMapper;
import com.model.sys.SysUser;

import freemarker.template.utility.StringUtil;

@Controller
public class WXController {

	static final Logger logger = LoggerFactory.getLogger(WXController.class);
	@Autowired
	private TaskMapper taskMapper;
	@Autowired
	PartsService partsService;
	@Autowired
	PartsDao partsDao;
	@Autowired
	PartsDetailDao partsDetailDao;
	@Autowired
	ProcessDao processDao;
	@Autowired
	WXService wxUtil;
	@Autowired
	private RedisUtils redisUtils;
	@Autowired
	private SysUserMapper sysUserMapper;

	@RequestMapping(value = "wx/getHI")
	@ResponseBody
	public String getHI(HttpServletRequest request) {
		// 通过微信code获取登录帐号
		String code = request.getParameter("code");
		code = code == null ? "" : code;
		String pwdString = request.getParameter("pwd");
		pwdString = pwdString == null ? "" : pwdString;
		String loginnameString = wxUtil.getUserId(code);
		return loginnameString;
	}

	@RequestMapping("wx/index")
	public String index(HttpServletRequest request) {
		return "wx/index";
	}

	@RequestMapping("wx/hello")
	public String hello(HttpServletRequest request) {
		return "wx/hello";
	}

	@RequestMapping("wx/qm/index")
	public String qmindex(HttpServletRequest request) {
		Map<String, Object> params = new HashMap<>();
		String status = request.getParameter("status");
		params.put("status", Global.NO + "");
		String search = request.getParameter("search");
		params.put("search", search);
		params.put("wxlimt", Global.YES);
		/*
		 * String pageTem = null
		 * !=request.getParameter("page")?request.getParameter("page").toString():"";
		 * String rowsTem =null !=
		 * request.getParameter("rows")?request.getParameter("rows").toString():""; if
		 * (pageTem != null) { int rows = Integer.parseInt(rowsTem); int page =
		 * Integer.parseInt(pageTem); int pageNum = (page - 1) * rows;
		 * params.put("pageNum", pageNum); params.put("pageRows", rows); }
		 */
		List<PartsModel> list0 = partsDao.selectList(params);
		request.setAttribute("list0", list0);

		String userid = request.getParameter("userid");
		SysUser sysUser = (SysUser) redisUtils
				.get(Global.REDIS_USER_PREFIX + UserTypeEnum.XYUser.getValue() + "-" + userid);
		params.put("status", Global.YES + "");
		if (sysUser != null && !sysUser.getSuperadmin().equals(Global.YES + "")) {
			params.put("updateId", sysUser.getUserid());
		}
		List<PartsModel> list1 = partsDao.selectList(params);
//		int count = partsDao.selectListCount(params);
		request.setAttribute("list1", list1);
//		request.setAttribute("count", count);
		request.setAttribute("userid", userid);

		return "wx/qm/index";
	}

	// 查询工序
	@RequestMapping("qm/queryProcess")
	public String queryProcess(HttpServletRequest request) {
		Map<String, Object> params = new HashMap<>();
		String parId = request.getParameter("parId");
		if (null != parId || !parId.equals(null)) {
			PartsModel partsModel = partsDao.load(parId);
			params.put("parId", parId);
			List<ProcessModel> processModels = processDao.selectList(params);
			request.setAttribute("partsModel", partsModel);
			request.setAttribute("processModels", processModels);
		}
		return "wx/qm/process";
	}

	// 查询特性列表
	@RequestMapping("qm/partItems")
	public String partItems(HttpServletRequest request) {
		Map<String, Object> params = new HashMap<>();
		String parId = request.getParameter("parId");
		String procId = request.getParameter("procId");
		PartsModel partsModel = partsDao.load(parId);
		ProcessModel process = processDao.load(procId, parId);
		params.put("procId", procId);
		params.put("parId", parId);
		List<PartsDetailModel> partDetails = partsDetailDao.selectList(params);
		request.setAttribute("partDetails", partDetails);
		request.setAttribute("partsModel", partsModel);
		request.setAttribute("processModel", process);
		return "wx/qm/partItem";
	}

	// 查询特性详情
	@RequestMapping("qm/partItemDetail")
	public String partItemDetail(HttpServletRequest request) {
		String parId = request.getParameter("parId");
		String deaId = request.getParameter("deaId");
		String procId = request.getParameter("procId");
		ProcessModel process = processDao.load(procId, parId);
		PartsModel partsModel = partsDao.load(parId);
		PartsDetailModel partDetail = partsDetailDao.loadUnique(deaId, procId, parId);
		request.setAttribute("processModel", process);
		request.setAttribute("partsModel", partsModel);
		request.setAttribute("partDetail", partDetail);
		return "wx/qm/partItemDetail";
	}

	@RequestMapping("wx/taskList")
	public String ChangePwd(HttpServletRequest request) {
		SysUser sysUser = (SysUser) request.getSession().getAttribute("sysUser");
		String puserString = JSON.toJSONString(sysUser);
		request.getSession().setAttribute("sysUser", sysUser);
		request.setAttribute("currentyear", DateUtils.getCurrentYear());
		request.setAttribute("currentweek", DateUtils.getWeek(new Date()));
		request.setAttribute("currentdate", DateUtils.getCurrentDate());
		HashMap<String, Object> hashtable = new HashMap<>();
		int week = DateUtils.getWeek(new Date());
		String year = DateUtils.getCurrentYear();
		hashtable.put("week", year + week);
		hashtable.put("userid", sysUser.getUserid());
		hashtable.put("username", sysUser.getUsername());
		List<HashMap<String, Object>> list = taskMapper.selectMytask(hashtable);
		request.setAttribute("taskList", list);
		return "wx/taskList";
	}

	@RequestMapping("/wx/taskDelay")
	public String delay(HttpServletRequest request) {
		String proId = request.getParameter("proId");
		String infId = request.getParameter("infId");
		String oldDate = request.getParameter("oldDate");
		request.setAttribute("proID", proId);
		request.setAttribute("infId", infId);
		request.setAttribute("oldDate", oldDate);
//		Map<String, String> hashMap = new HashMap<>();
//		SysUser sysUser = (SysUser) request.getSession().getAttribute("sysUser");
//		HashMap<String, Object> hashtable = new HashMap<>();
//		int week = DateUtils.getWeek(new Date());
//		String year = DateUtils.getCurrentYear();
//		hashtable.put("week", year + week);
//		hashtable.put("userid", sysUser.getUserid());
//		hashtable.put("username", sysUser.getUsername());
//		hashtable.put("proId", proId);
//		hashtable.put("mobile", "0");
//		List<HashMap<String, Object>> list = taskMapper.selectMytask(hashtable);
//		if (list.size() > 0) {
//			request.setAttribute("map1", list.get(0));
//		} else {
//			request.setAttribute("map1", hashMap);
//		}
		return "wx/taskDelay";
	}

	@RequestMapping("/wx/taskAdd")
	public String taskDefault(HttpServletRequest request) {
		String item;
		item = request.getParameter("item");
		item = item.substring(1, item.length() - 1);
		Map<String, String> hashMap = StringUtils.getStringToMap(item);
		SysUser sysUser = (SysUser) request.getSession().getAttribute("sysUser");
		String proId = hashMap.get("PRO_ID");
		HashMap<String, Object> hashtable = new HashMap<>();
		int week = DateUtils.getWeek(new Date());
		String year = DateUtils.getCurrentYear();
		hashtable.put("week", year + week);
		hashtable.put("userid", sysUser.getUserid());
		hashtable.put("username", sysUser.getUsername());
		hashtable.put("proId", proId);
		hashtable.put("mobile", "1");
		List<HashMap<String, Object>> list = taskMapper.selectMytask(hashtable);
		if (list.size() > 0) {
			request.setAttribute("map1", list.get(0));
		} else {
			request.setAttribute("map1", hashMap);
		}
		return "wx/taskAdd";
	}
}
